package cn.com.kh.result;

import net.sf.json.JSONObject;

import java.io.Serializable;

/**
 * @projectName: ngsapitask-davco
 * @package: com.elite.groovy.kh
 * @className: apiResult
 * @author: LiJT
 * @description: TODO
 * @date: 2023-07-10 15:24
 * @version: 1.0
 */
public class iAPIResult implements Serializable {
    public static final int ERROR = -1;
    public static final int WARN = 0;
    public static final int SUCCESS = 1;
    private int code;
    private String msg;
    private Object data;

    public iAPIResult() {
        this.code = -1;
    }

    public iAPIResult(int code) {
        this.code = code;
    }

    public iAPIResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public iAPIResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * @param msg:结果描述
     * @param data:返回数据对象
     * @return JSONObject
     * @author LiJT
     * @description 统一接口返回-成功
     * @date 2024-07-25 09:47
     */
    public static JSONObject SUCCESS(String msg, Object data) {
        return JSONObject.fromObject(new iAPIResult(iAPIResult.SUCCESS, msg, data));
    }

    /**
     * @param msg: 返回结果描述
     * @return JSONObject
     * @author LiJT
     * @description 统一接口返回-警告
     * @date 2024-07-25 09:47
     */
    public static JSONObject WARN(String msg) {
        return JSONObject.fromObject(new iAPIResult(iAPIResult.WARN, msg));
    }

    public static JSONObject WARN(String msg, Object data) {
        return JSONObject.fromObject(new iAPIResult(iAPIResult.WARN, msg, data));
    }

    /**
     * @param msg: 返回结果描述
     * @return JSONObject
     * @author LiJT
     * @description 统一接口返回-错误
     * @date 2024-07-25 09:47
     */
    public static JSONObject ERROR(String msg) {
        return JSONObject.fromObject(new iAPIResult(iAPIResult.ERROR, msg));
    }

    public static JSONObject ERROR(String msg, Object data) {
        return JSONObject.fromObject(new iAPIResult(iAPIResult.ERROR, msg, data));
    }

    /**
     * @param code: 返回结果码
     * @param msg:  返回结果描述
     * @return JSONObject
     * @description 统一接口返回-自定义(无结果)
     */
    public static JSONObject GetRES(int code, String msg) {
        return JSONObject.fromObject(new iAPIResult(code, msg));
    }

    /**
     * @param code: 返回结果码
     * @param msg:  返回结果描述
     * @return JSONObject
     * @description 统一接口返回-自定义(有结果)
     */
    public static JSONObject GetRES(int code, String msg, Object data) {
        return JSONObject.fromObject(new iAPIResult(code, msg, data));
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[CODE:").append(this.code).append("]");
        if (this.msg != null) {
            sb.append("[MSG:").append(this.msg).append("]");
        }

        if (this.data != null) {
            sb.append("[DATA:").append(this.data.toString()).append("]");
        }

        return sb.toString();
    }
}